package com.sinya.shoppingmall.widget;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.nineoldandroids.animation.Animator;
import com.sinya.shoppingmall.R;

import animationslibrary.AnimationHelp;
import animationslibrary.Techniques;

/**
 * @author KoizumiSinya
 * @date 2015/12/30.
 */
public class SearchView extends LinearLayout {

    private Context mContext;

    private EditText editText;
    private TextView hintText;
    private LinearLayout editLayout;
    private RelativeLayout hintLayout;
    private TextView btn;
    private ImageView clear;

    private View titleView, outView, showView, rootView;

    private onSearchInputListener inputListener;

    public SearchView(Context context) {
        super(context);
    }

    public SearchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;

        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_search_layout, this);

        initViews();
        setListener();
    }

    private void initViews() {
        hintLayout = (RelativeLayout) findViewById(R.id.rl_search_hint);
        editLayout = (LinearLayout) findViewById(R.id.ll_search_edit);

        hintText = (TextView) findViewById(R.id.tv_search_hint);
        editText = (EditText) findViewById(R.id.et_input);

        btn = (TextView) findViewById(R.id.btn_cancel);
        clear = (ImageView) findViewById(R.id.btn_clear);
        clear.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                editText.setText("");
                clear.setVisibility(View.INVISIBLE);
            }
        });
    }

    private void setListener() {
        hintLayout.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hideTitleView(Techniques.FadeOut, 500, true);
                hintLayout.setVisibility(View.GONE);
                editLayout.setVisibility(View.VISIBLE);
                if (inputListener != null) {
                    inputListener.onClickSearch();
                }
            }
        });

        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!TextUtils.isEmpty(s.toString())) {
                    clear.setVisibility(View.VISIBLE);
                    btn.setText("搜索");
                } else {
                    clear.setVisibility(View.INVISIBLE);
                    btn.setText("取消");
                }

                if (inputListener != null) {
                    inputListener.editTextChanged();
                }
            }
        });

        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (inputListener != null) {
                    if (btn.getText().equals("搜索")) {
                        inputListener.onSearch();
                    } else if (btn.getText().equals("取消")) {
                        inputListener.onCancel();

                        initDefalutSearchView();
                        //将之前有隐藏的View 再次拉回
                        hideTitleView(Techniques.FadeIn, 500, false);
                    }
                }
            }
        });
    }

    // [+] 对外提供方法 修改内容参数 和 实现接口

    /**
     * 设置启动搜索框时，传入外部要加入动画的控件
     *
     * @param hideView  要隐藏的控件
     * @param outView   要向外移开的控件
     * @param showView  将要显示的控件
     * @param container 外层根布局
     */
    public void setHideView(View hideView, View outView, View showView, View container) {
        this.titleView = hideView;
        this.outView = outView;
        this.showView = showView;
        this.rootView = container;
    }

    /**
     * 设置搜索框初始化状态时 显示的Hint文字
     *
     * @param strHint
     */
    public void setHintText(String strHint) {
        hintText.setText(strHint);
    }

    /**
     * 获取搜索框输入的文字
     *
     * @return
     */
    public String getInputText() {
        return editText.getText().toString();
    }

    /**
     * 恢复搜索控件初始化状态，并收回输入法
     */
    public void initDefalutSearchView() {
        editText.setText("");
        hintLayout.setVisibility(View.VISIBLE);
        editLayout.setVisibility(View.GONE);

        //收回输入框
        InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }

    public void setSearchListener(onSearchInputListener listener) {
        this.inputListener = listener;
    }

    // [-] 对外提供方法 修改内容参数

    private void hideTitleView(final Techniques anim, final int time, final boolean close) {
        if (titleView == null) {
            return;
        }

        Animation translateAnimation = new TranslateAnimation(0, 0, close ? 0 : -titleView.getHeight(), close ? -titleView.getHeight() : 0);
        translateAnimation.setDuration(time - 10);
        rootView.startAnimation(translateAnimation);
        translateAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animator) {
                if (!close) {
                    titleView.setVisibility(View.VISIBLE);
                } else {
                    titleView.setVisibility(View.GONE);
                }
            }

            @Override
            public void onAnimationRepeat(Animation animator) {
            }

            @Override
            public void onAnimationEnd(Animation animator) {
                if (outView == null) {
                    return;
                }
                AnimationHelp.AnimationOperation rope = AnimationHelp.with(anim).duration(time)//
                        .interpolate(new AccelerateDecelerateInterpolator())//
                        .withListener(new Animator.AnimatorListener() {
                            @Override
                            public void onAnimationStart(Animator animator) {
                                if (!close) {
                                    outView.setVisibility(View.VISIBLE);
                                    showView.setVisibility(View.GONE);
                                }
                            }

                            @Override
                            public void onAnimationEnd(Animator animator) {
                                if (close) {
                                    outView.setVisibility(View.GONE);
                                    showView.setVisibility(View.VISIBLE);
                                }
                            }

                            @Override
                            public void onAnimationCancel(Animator animator) {
                            }

                            @Override
                            public void onAnimationRepeat(Animator animator) {
                            }
                        }).playOn(outView);
            }
        });

    }


    // [+] 对外提供接口

    public interface onSearchInputListener {
        public void editTextChanged();

        public void onCancel();

        public void onSearch();

        public void onClickSearch();
    }

    // [-] 对外提供接口

}
